package hibernate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import modelo.*;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class HibernateDaoHorasMateria {
	private static HibernateDaoHorasMateria instancia;
	private static SessionFactory sf;
	
	public static HibernateDaoHorasMateria getInstancia(){
		if(instancia == null) {
			instancia = new HibernateDaoHorasMateria();
			sf = HibernateUtil.getSessionFactory();
			
		}
		return instancia;
	}
	
	@SuppressWarnings("rawtypes")
	public List<Materia> traerMateriasDelCurso(Curso c){
		Session session = sf.openSession();
		int i = c.getId();
		
		List<Materia> mate = new ArrayList<Materia>();
		Iterator it =session.createQuery("select m.nombre From Curso c join c.horasmaterias hm join hm.materia m where c.Id_Curso =(:id)").setParameter("id", i).iterate();
		
		while(it.hasNext())
		{
			String tupla = (String) it.next();
			
			String nombre = tupla;
			
			Materia m = new Materia(nombre);
			mate.add(m);
		}
		
		
		return mate;
			
	}
}
